package top.ruanlitao.shortlink.project.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import top.ruanlitao.shortlink.project.common.constant.SentinelRuleResourceConstant;
import top.ruanlitao.shortlink.project.handler.CustomBlockHandler;
import top.ruanlitao.shortlink.project.service.ShortLinkService;

import java.util.Objects;

@RestController
@RequiredArgsConstructor
public class ShortLinkGotoController {

    private final ShortLinkService shortLinkService;

    /**
     * 短链接跳转原始链接
     */
    @GetMapping("/{short-uri}")
    @SentinelResource(
            value = SentinelRuleResourceConstant.GOTO_LINK,
            blockHandler = "gotoShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    public void restoreUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response) {
        // 过滤掉 favicon.ico
        if (Objects.equals("favicon.ico", shortUri)) {
            return;
        }
        shortLinkService.restoreUrl(shortUri, request, response);
    }
}
